#include "StdAfx.h"
#include "QuoteSource.h"
#include "RecvThread.h"
#include <Shlwapi.h>

CQuoteSource::CQuoteSource( HWND hWnd, UINT uMsg )
{
	m_pThread = AfxBeginThread( RUNTIME_CLASS(CReceivingThread) );
	m_pThread->PostThreadMessage( CReceivingThread::WM_SETHANDLER, (WPARAM)hWnd, (LPARAM)uMsg );
}

CQuoteSource::~CQuoteSource(void)
{
	Quitthread();
}

void CQuoteSource::Quitthread( void )
{
	if( m_pThread != NULL )
	{
		m_pThread->ResumeThread();
		m_pThread->PostThreadMessage( WM_QUIT, 0, 0 );

		DWORD dwWait = ::WaitForSingleObject( m_pThread->m_hThread, 3*1000 );
		ASSERT( WAIT_OBJECT_0 == dwWait  || WAIT_FAILED == dwWait );
	}
}

bool CQuoteSource::Connect( LPCTSTR strIP, LPCTSTR strPort )
{
	CStringA str( strIP );
	ULONG lIP = ::inet_addr( str );
	if( 0 == lIP )
	{
		return false;
	}

	USHORT sPort = (USHORT)::StrToInt( strPort );
	sPort = ::htons( (USHORT)sPort );
	m_pThread->PostThreadMessage( CReceivingThread::WM_CONNECT, lIP, sPort );

	return true;
}